//
//  ViewController.swift
//  photoSelector
//
//  Created by 大波 on 16/3/11.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit

class DBPhotoSelectorController: UICollectionViewController {
    
    let flowLayout = UICollectionViewFlowLayout()
    
    let reuseIdentifier = "reuseIdentifier"
    
    var currentIndexPath: NSIndexPath?
    
    let maxCount = 6
    
    var photos = [UIImage]()
    
    init() {
        
        super.init(collectionViewLayout: flowLayout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareCollectionView()
    }
    
    
    /// 设置collectionView布局
    private func prepareCollectionView() {
        
        let WH = 80
        
        let margin: CGFloat = 10
        
        flowLayout.itemSize = CGSize(width: WH, height: WH)
        
        flowLayout.minimumLineSpacing = margin
        
        flowLayout.minimumInteritemSpacing = margin
        
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: margin, bottom: 0, right: margin)
        
        collectionView?.registerClass(DBPhotoSeletorCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        collectionView?.backgroundColor = UIColor.whiteColor()
    }
}


// MARK: - UICollectionViewDataSource

extension DBPhotoSelectorController: DBPhotoSeletorCellDelegate {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return photos.count < maxCount ? photos.count + 1 : photos.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! DBPhotoSeletorCell
        
        cell.backgroundColor = UIColor.lightGrayColor()
        
        cell.photoSeletorCellDelegate = self
        
        if indexPath.item < photos.count {
            
            cell.image = photos[indexPath.item]
            
        } else {
            
            cell.setAddButton()
        }
        
        return cell
    }
}


// MARK: - UIImagePickerControllerDelegate, UINavigationControllerDelegate 代理方法

extension DBPhotoSelectorController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        let newImage = image.scaleImage()
        
        if currentIndexPath!.item == photos.count {
            
            photos.append(newImage)
            
        } else {
            
            photos[currentIndexPath!.item] = newImage
        }
        
        collectionView?.reloadData()
        
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}


// MARK: - DBPhotoSeletorCellDelegate

extension DBPhotoSelectorController {
    
    func addButtonDidClick(cell: DBPhotoSeletorCell, button: UIButton) {
        
        let photoVC = UIImagePickerController()
        
        photoVC.delegate = self
        
        currentIndexPath = collectionView?.indexPathForCell(cell)
        
        presentViewController(photoVC, animated: true, completion: nil)
    }
    
    func deletedButtonDidClick(cell: DBPhotoSeletorCell, button: UIButton) {
        
        let indexPath = collectionView?.indexPathForCell(cell)
        
        photos.removeAtIndex(indexPath!.item)
        
        collectionView?.reloadData()
    }
}




















